package com.wlj.security.core.social.weixin.config;

import com.wlj.security.core.social.view.WljConnectView;
import com.wlj.security.core.social.weixin.connect.WeixinConnectionFactory;
import com.wlj.security.core.properties.WljSecurityProperties;
import com.wlj.security.core.properties.WeixinProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.social.SocialAutoConfigurerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.web.servlet.View;

@Configuration
@ConditionalOnProperty(prefix = "wlj.security.social.weixin",name = "app-id")
public class WeixinAutoConfig extends SocialAutoConfigurerAdapter {

    @Autowired
    private WljSecurityProperties wljSecurityProperties;

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {
        WeixinProperties weixin = wljSecurityProperties.getSocial().getWeixin();
        return new WeixinConnectionFactory(weixin.getProviderId(),weixin.getAppId(),weixin.getAppSecret());
    }

    //connect/weixinConnected:微信社交账号绑定成功返回的视图，其实也是bean
    //connect/weixinConnect :解绑成功返回的视图
    @Bean({"connect/weixinConnected","connect/weixinConnect"})
    @ConditionalOnMissingBean(name = "weixinConnectedView")     //用户可配置此bean覆盖下面默认view
    public View weixinConnectedView(){
        return new WljConnectView(); //绑定,解绑成功返回同一view
    }
}
